from enthought.traits.ui.api import Handler


class GprHandler(Handler):

    #---------------------------------------------------------------------------
    # State traits
    #---------------------------------------------------------------------------

    gpr = Instance(HUBRA)
    view = Instance(PlotUI)

    #---------------------------------------------------------------------------
    # Handler interface
    #---------------------------------------------------------------------------

    def init(self, info):
        self.view = info.object.view

    #---------------------------------------------------------------------------
    # Public Controller interface
    #---------------------------------------------------------------------------
    
    ### File Menu
    def open_file(self, ui_info):
        print "That should load a file"
        from gprViewerUIs import wxOpenFile
        file, wdir = wxOpenFile()
        print file, wdir
        show_plot(file, wdir)

    ### Edit Menu
    def edit_plot(self, ui_info):
        self.view.configure_traits(view="plot_edit_view")
        
    ### View Menu
    def show_trackplot(self, ui_info):
        from gprViewerTools import TrackPlot
        tp = TrackPlot(self.view.gpr.coords.x, self.view.gpr.coords.y, self.view.gpr.coords.elev)
        tp.edit_traits()
        
    def show_metadata(self, ui_info):
        pass
    
    def show_skindepth(self, ui_info):
        if ui_info.ui.context["object"].skindepth_show:
            self.view.pickdata.set_data("refy", array([]))
            self.view.pickdata.set_data("refx", array([]))
            self.view.pickdata.set_data("sky", array([]))
            self.view.pickdata.set_data("skx", array([]))
            ui_info.ui.context["object"].skindepth_show = not ui_info.ui.context["object"].skindepth_show
        else:
            traces = self.view.gpr.traces
            refndxmin = 130
            refndxmax = 225
            refndx = zeros((traces,1))
            max = zeros((traces,1))
            skindx = zeros((traces,1))
            skindepth = zeros((traces,1))
            refdepth = zeros((traces,1))
            for n in range(traces):
                trace = self.view.gpr.data[:,n]
                max[n,0] = trace[refndxmin:refndxmax].max()
                refndx[n,0] = where(trace[refndxmin:refndxmax]==trace[refndxmin:refndxmax].max())[0][-1] + refndxmin
                sd = max[n,0]+10*log10(exp(-1))
                skindx[n,0] = where(trace>sd)[0][-1]
#                skintime = (skindx[n,0]+topcut)*self.view.gpr.deltaT*1e9
                skindepth[n,0] = self.view.gpr.t[skindx[n,0]] # in ns
                refdepth[n,0] = self.view.gpr.t[refndx[n,0]] # in ns

            self.view.pickdata.set_data("refy", refdepth.reshape((refdepth.size,)))
            self.view.pickdata.set_data("refx", self.view.gpr.tracendx.reshape((self.view.gpr.tracendx.size,)))
            self.view.pickdata.set_data("sky", skindepth.reshape((skindepth.size,)))
            self.view.pickdata.set_data("skx", self.view.gpr.tracendx.reshape((self.view.gpr.tracendx.size,)))           
            ui_info.ui.context["object"].skindepth_show = not ui_info.ui.context["object"].skindepth_show
        
        
    ### Tool Menu
    def activate_picktool(self, ui_info):
        self.view.picktool.enable()
        
    def activate_traceselecttool(self, ui_info):
        self.view.traceselecttool.enable()
        
    def activate_zoomtool(self, ui_info):
        self.view.zoomtool.enable()
        
     
#    def activate_traceselecttool(self, ui_info):
#        self.view.traceselecttool = RangeSelection(component=self.view.cmapplot,
##                                                   left_button_selects=False,
##                                                   constrain=True,
##                                                   constrain_direction="x"
#                                                   )
#        self.view.cmapplot.tools.append(self.view.traceselecttool)
#        self.view.traceselectoverlay = RangeSelectionOverlay(component=self.view.cmapplot, axis="value",)
#        self.view.cmapplot.overlays.append(self.view.traceselectoverlay)  
        